import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { TriggerTableComponent } from "../../../../shared-component/trigger-table/trigger-table.component";
import { Function } from "../../../../shared-component/class/instance";
import { Environment } from "environments/environment";

@Component({
  selector: 'app-function-trigger',
  templateUrl: './function-trigger.component.html',
  styleUrls: ['./function-trigger.component.less']
})
export class FunctionTriggerComponent implements OnInit {
  @ViewChild('triggerTable', { static: true }) triggerTable: TriggerTableComponent;
  @Input() groupId = '';
  @Input() functionId = '';
  @Input() fcsInstance: Function;
  @Input() groupDisplayName: string;
  isDetail = true;
  triggerTypeList: Array<any> = Environment.application.triggerTypeList;
  triggerName: string = '';
  triggerType = 'all';

  constructor() { }

  ngOnInit() {
    this.triggerTypeChange();
  }

  triggerTypeChange() {
    this.triggerTable.type = this.triggerType;
    this.triggerTable.getTriggerList(this.groupId, this.functionId);
  }

  createTrigger() {
    this.triggerTable.groupDisplayName = this.groupDisplayName;
    this.triggerTable.createTrigger(this.fcsInstance);
  }

  refreshReset() {
    this.triggerType = 'all';
    this.triggerName = '';
    this.refreshTriggerList();
  }

  refreshTriggerList() {
    this.triggerTable.type = this.triggerType;
    this.triggerTable.getTriggerList(this.groupId, this.functionId);
  }
  resetTriggerTypeList(event) {
    if (event && event['triggerTypes']) {
      this.triggerTypeList = event['triggerTypes'];
    }
  }
}
